package com.DosonLeung.homeschoolcommunication.Models;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.DosonLeung.homeschoolcommunication.Constant.UserConstant;
import com.DosonLeung.homeschoolcommunication.Controllers.LoginActivity;
import com.DosonLeung.homeschoolcommunication.Utils.StorageUtil;

import static com.DosonLeung.homeschoolcommunication.Utils.StorageUtil.deleteFile;

/**
 * Created by DosonLeung on 2014/10/21.
 */
public class SystemModel {
    public static void logOut(Activity app){
        try {
            deleteFile(StorageUtil.getInfoDir()+"/"+"Info.json");//希望清除
            deleteFile(StorageUtil.getChatDir().toString()+"/"+"members.db3");
            UserConstant.clearUserConstant();
            setLoginStatus(app,false);//将自动登录取消
            Intent intent = new Intent(app, LoginActivity.class);
            app.startActivity(intent);
            app.finish();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void logOutButRememberMe(Activity app){
        try {
            deleteFile(StorageUtil.getChatDir().toString()+"/"+"members.db3");
            UserConstant.clearUserConstant();
            setLoginStatus(app,false);//将自动登录取消
            Intent intent = new Intent(app, LoginActivity.class);
            app.startActivity(intent);
            app.finish();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void clearHistory(Activity app) {
        if (StorageUtil.deleteFileFolder(StorageUtil.getChatDir().toString())
            &&StorageUtil.deleteFileFolder(StorageUtil.getCameraDir().toString())
            &&StorageUtil.deleteFileFolder(StorageUtil.getVoiceDir().toString())
            &&StorageUtil.deleteFileFolder(StorageUtil.getInfoDir().toString())) {
            UserConstant.clearUserConstant();
            setLoginStatus(app,false);//将自动登录取消
            Toast.makeText(app, "清空历史记录成功", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(app, LoginActivity.class);
            app.startActivity(intent);
            app.finish();
        }
        else
            Toast.makeText(app, "删除文件失败", Toast.LENGTH_SHORT).show();
    }

    public static void autoLogin(Activity app,boolean isChecked){
        setLoginStatus(app,isChecked);
    }

    //自动登录为true 非自动登录为false
    public static boolean getLoginStatus(Activity app) {
        SharedPreferences preferences = app.getSharedPreferences("login", Context.MODE_PRIVATE);
        boolean status = preferences.getBoolean("login",false);
        return status;
    }

    private static void setLoginStatus(Activity app,boolean status){
        SharedPreferences preferences = app.getSharedPreferences("login", Context.MODE_PRIVATE);
        //boolean status = preferences.getBoolean("login", false);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.putBoolean("login", status);
        editor.commit();
    }
}
